#include <Arduino.h>

#define BLINKER_MIOT_LIGHT //启用小爱同学电源类操作接口
#define BLINKER_WIFI
#include <Blinker.h>
#include "Adafruit_NeoPixel.h"   //RGB灯带库
int flag=0;
#define PIN  0//灯带GPIO
#define NUM_LEDS  12   //灯珠数
#define BRIGHTNESS 200 //亮度
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN,  NEO_KHZ800 + NEO_RGB); //设置RGB灯带的参数

char auth[] = "93e8fadc01f5";
char ssid[] = "613专属";
char pswd[] = "613613613";
//char ssid[] = "leisure";
//char pswd[] = "shadow27";

BlinkerButton Button1("btn");  //按钮组件
BlinkerRGB RGB1("rgb"); //颜色拾取组件
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
    for (uint16_t i = 0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, c);
        strip.show();
        delay(wait); //延迟1ms，便于缓冲数据
    }
}
// 设置按键对象
void button1_callback(const String & state)
{
    
    BLINKER_LOG("get button state: ", state); 
    Blinker.vibrate(); 
    if(flag == 0)
  {
    flag=1; 
    colorWipe(strip.Color(0, 0, 0, 255), 1); // 设置灯条关，黑色，,参数 R G B W
    }
    else
  {
      flag = 0;
      colorWipe(strip.Color(255,222,173,200), 1); //设置灯条开，白色,参数 R G B W
  }
}
//设置颜色拾取器的对象
void rgb1_callback(uint8_t r_value, uint8_t g_value, uint8_t b_value, uint8_t bright_value)
{
    digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
    BLINKER_LOG("R value: ", r_value);
    BLINKER_LOG("G value: ", g_value);
    BLINKER_LOG("B value: ", b_value);
    BLINKER_LOG("Rrightness value: ", bright_value);

    uint8_t colorR = map(r_value, 0, 255, 0, bright_value);
    uint8_t colorG = map(g_value, 0, 255, 0, bright_value);
    uint8_t colorB = map(b_value, 0, 255, 0, bright_value);

    colorWipe(strip.Color( colorG,colorR, colorB,150), 1); // 更改颜色
}

//小爱电源控制回调函数,在小爱训练里面添加开关灯时,会进入到这里
void miotPowerState(const String & state)
{
    BLINKER_LOG("need set power state: ",state);

    if (state == BLINKER_CMD_OFF) {     //如果语音接收到是关闭灯
        colorWipe(strip.Color(0, 0, 0, 255), 1);  //关闭灯
        BlinkerMIOT.powerState("off");  //返回数据到小爱,否则小爱会报错
        BlinkerMIOT.print();
    }
    else if (state == BLINKER_CMD_ON) {
        BlinkerMIOT.powerState("on");
        BlinkerMIOT.print();
        colorWipe(strip.Color(255,222,173,200), 1); 
    }
}
//小爱颜色控制回调函数,对小爱说把灯调成x色,就会到这里来执行
void miotColor(int32_t color)
{
    uint8_t colorR,colorG,colorB;
    BLINKER_LOG("need set color: ", color);

    colorR = color >> 16 & 0xFF;
    colorG = color >>  8 & 0xFF;
    colorB = color       & 0xFF;

    BLINKER_LOG("colorR: ", colorR, ", colorG: ", colorG, ", colorB: ", colorB);
    colorWipe(strip.Color(colorG,colorR, colorB,100), 1); //修改灯条颜色
    strip.show();

    BlinkerMIOT.color(color);
    BlinkerMIOT.print();
}
//小爱亮度的控制接口
void miotBright(const String & bright)
{
    BLINKER_LOG("need set brightness: ", bright);

    uint8_t colorW = bright.toInt();
    
    BLINKER_LOG("now set brightness: ", colorW);
    
    strip.setBrightness(colorW*15/10+105);//因为函数设置的亮度是0-255,小爱接口的是1-100
    BlinkerMIOT.brightness(colorW);
    strip.show();
    BlinkerMIOT.print();
}
//小爱切换模式,对小爱说把灯调到XX模式,就会执行相应的操作
void miotMode(uint8_t mode)
{
    BLINKER_LOG("need set mode: ", mode);
    
    if (mode == BLINKER_CMD_MIOT_DAY) { //日光模式
       colorWipe(strip.Color(255, 255, 255 
       , 255), 1); // 白色
    }
    else if (mode == BLINKER_CMD_MIOT_NIGHT) {//月光模式
        
        colorWipe(strip.Color(237, 199, 204), 1); // White
    }
    else if (mode == BLINKER_CMD_MIOT_COLOR) {   //彩光模式
        // Your mode function
    }
    else if (mode == BLINKER_CMD_MIOT_WARMTH) {   //温馨模式
        // Your mode function
    }
    else if (mode == BLINKER_CMD_MIOT_TV) {   //电视模式
        // Your mode function
    }
    else if (mode == BLINKER_CMD_MIOT_READING) {  //阅读模式
        // Your mode function
    }
    else if (mode == BLINKER_CMD_MIOT_COMPUTER) {   //电脑模式
        // Your mode function
    }
    BlinkerMIOT.mode(mode);
    BlinkerMIOT.print();
}

//初始化
void setup()
{
    // 初始化串口
    Serial.begin(115200);
    BLINKER_DEBUG.stream(Serial);
    BLINKER_DEBUG.debugAll();
    Blinker.begin(auth, ssid, pswd);   // 初始化blinker，进行网络连接

    Button1.attach(button1_callback);  //按键回调函数
    RGB1.attach(rgb1_callback); //颜色拾取器回调函数
    
    BlinkerMIOT.attachPowerState(miotPowerState); //电源类回调函数
    BlinkerMIOT.attachColor(miotColor);         //小爱颜色选择回调函数
    BlinkerMIOT.attachBrightness(miotBright);   //亮度回调函数
    BlinkerMIOT.attachMode(miotMode);         //灯的模式选择回调函数
    
    strip.setBrightness(BRIGHTNESS);  //设置灯条亮度，亮度设置为初始值
    strip.begin();                    //灯条执行初始化
    strip.show(); // 显示应用的值
}

void loop() {
    Blinker.run();
}

